<?php
use Homework5\Maze;
error_reporting(E_ALL);
include index.php;



die();
// Create a blank image
const TOP 			= 'top';
const TOPLEFT 		= 'topleft';
const TOPRIGHT 		= 'topright';
const BOTTOMLEFT	= 'bottomleft';
const BOTTOMRIGHT 	= 'bottomright';
const BOTTOM 		= 'bottom';

$positions = array (
	0 => array (
		-1 	=> TOPLEFT,
		1 	=> TOPRIGHT
	),
	1 => array (
		0 	=> BOTTOM,
		-1 	=> BOTTOMLEFT,
		1 	=> BOTTOMRIGHT
	),
	-1 => array (
		0 	=> TOP
	)
);

$image = imagecreatetruecolor(1360,768);

// Allocate a color for the polygon
$background = imagecolorallocate($image, 255, 255, 255);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

imagefilledrectangle($image, 0, 0, 1360, 768, $background);
// Draw the polygon
$radius = 50;
$width = 2 * $radius;
$side = 1.5 * $radius;

$elements = array();

for ($i = 0; $i < 6; $i++) {
	for ($j = 0; $j < 6; $j++) {
		//$height = $radius * sin(deg2rad(60));
		//if ($j % 2 == 0) {
		$height = $i * 2 * $radius * sin(deg2rad(60));
		//}
		if ($j % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
		
		imagepolygon($image, array (
	      0.5 * $radius + $j * $side, $height,
	      1.5 * $radius + $j * $side, $height,
	      2 * $radius + $j * $side, $radius * sin(deg2rad(60)) + $height,
	      1.5 * $radius + $j * $side, 2 * $radius * sin(deg2rad(60)) + $height,
	      0.5 * $radius + $j * $side, 2 * $radius * sin(deg2rad(60)) + $height,
	      0 + $j * $side, $radius * sin(deg2rad(60)) + $height
			), 6, $black);
			
			
		$elements[] = array($j, $i);
	}
}

drawLine($image, 2, 3, BOTTOMRIGHT);
//imageline($image, 100, 0, 300, 300, $white);
function drawLine($image, $x, $y, $position) {
	global $radius, $width, $side, $white;
	imagesetthickness($image, 2);
	
	if ($position == TOP) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side + ($width - $side);	
		$y1 = $y2 = $height;
		$x2 = $x1 + $side - ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	} else if ($position == TOPLEFT) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side;	
		$y2 = $height;
		$y1 = $height + $radius * sin(deg2rad(60));
		$x2 = $x1 + ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	} else if ($position == TOPRIGHT) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side + $side;	
		$y1 = $height;
		$y2 = $height + $radius * sin(deg2rad(60));
		$x2 = $x1 + ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	} else if ($position == BOTTOM) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side + ($width - $side);	
		$y1 = $y2 = $height + 2 * $radius * sin(deg2rad(60));
		$x2 = $x1 + $side - ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	} else if ($position == BOTTOMLEFT) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side;	
		$y1 = $height + $radius * sin(deg2rad(60));
		$y2 = $height + 2 * $radius * sin(deg2rad(60));
		$x2 = $x1 + ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	} else if ($position == BOTTOMRIGHT) {
		$height = $x * 2 * $radius * sin(deg2rad(60));
		if ($y % 2 != 0) {
			$height = $height + $radius * sin(deg2rad(60));
		}
	
		$x1 = $y * $side + $side;	
		$y2 = $height + $radius * sin(deg2rad(60));
		$y1 = $height + 2 * $radius * sin(deg2rad(60));
		$x2 = $x1 + ($width - $side);
		
		imageline($image, $x1, $y1, $x2, $y2, $white);
	}
}

// Output the picture to the browser
header('Content-type: image/png');

imagepng($image);
imagedestroy($image);
?>